# apps/roles/views.py
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, permissions

from .filters import RoleFilter
from .models import Role
from .serializers import RoleSerializer
from ..common.mixins import TenantViewSetMixin


class RoleViewSet(TenantViewSetMixin, viewsets.ModelViewSet):
    queryset = Role.objects.all().order_by('order')
    serializer_class = RoleSerializer
    permission_classes = [permissions.IsAdminUser]
    filter_backends = [DjangoFilterBackend]
    filterset_class = RoleFilter  # 使用过滤器


    def get_serializer(self, *args, **kwargs):
        # 处理嵌套数据
        serializer = super().get_serializer(*args, **kwargs)
        if isinstance(serializer, RoleSerializer) and self.action in ['update', 'create']:
            # 处理 menus 字段
            pass
        return serializer


    def perform_create(self, serializer):
        super().perform_create(serializer)


    def perform_update(self, serializer):
        super().perform_update(serializer)
